<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'
import {Delete} from '@element-plus/icons-vue'
import { getAllShowService, getShowByClassifyService, insertShowService, deleteShowService } from '@/api/show'
import { getAllEthnicService } from '@/api/ethnic'

const tableData = reactive([])

// const search = ref('')
// const filterTableData = computed(() =>
//     tableData.filter(
//         (data) =>
//             !search.value ||
//             data.detail.toLowerCase().includes(search.value.toLowerCase()) || data.classify.toLowerCase().includes(search.value.toLowerCase())
//     )
// )

const pageNum = ref(1)
const total = ref(20)
const pageSize = ref(10)

async function getShowList() {
    tableData.length = 0
    let result = await getAllShowService(pageNum.value, pageSize.value)
    // if (result.code === 0) {
    total.value = result.data.total
    tableData.values = result.data
    for (let i = 0; i < result.data.items.length; i++) {
        tableData.push(result.data.items[i])
    }
    console.log(tableData)
}
getShowList()


const onSizeChange = (size) => {
    pageSize.value = size
    getShowList()
}
const onCurrentChange = (num) => {
    pageNum.value = num
    getShowList()
}

const drawer = ref(false)

const form = reactive({
    detail: '',
    url: '',
    classify: ''
})

const nationalities = ref([])

const openDrawer = async () => {
    drawer.value = true
    let result = await getAllEthnicService();
    nationalities.value = result.data
}

const beforeAvatarUpload = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('请上传图片！')
        return false
    } else if (rawFile.size / 1024 / 1024 > 10) {
        ElMessage.error('文件大于10MB!')
        return false
    }
    return true
}

const handleRemove = (uploadFile, uploadFiles) => {
    console.log(uploadFile, uploadFiles)
}

const uploadSuccess = (res) => {
    ElMessage.success("上传成功！")
    form.url = res.data
}
const uploadError = () => {
    ElMessage.error("上传失败！")
}

const rules = {
    detail: [{ required: true, message: '描述不可为空！', trigger: 'blur' }],
    url: [{ required: true, message: '图片不可为空！', trigger: 'blur' }],
    classify: [{ required: true, message: '民族分类必选！', trigger: 'blur' }]
}

const cancelClick = async () => {
    //----------清空form表单
    form.detail = ''
    form.url = ''
    form.classify = ''
    drawer.value = false
}

const confirmClick = async () => {
    await insertShowService(form)
    ElMessage.success("新建民族文化展品成功")
    cancelClick()
    getShowList()
}

const deleteShow = async (id) => {
    await deleteShowService(id)
    ElMessage.success("删除民族文化展品成功")
    getShowList()
}

</script>



<template>
    <div style="padding: 30px;width: 100%;">
        <el-button type="primary" style="margin-left: 16px" @click="openDrawer">发布展品</el-button>

        <div class="container">
            <div class="item" v-for="item in tableData">
                <div class="mask-div">
                    <el-image style="height: 220px; " :src="item.url" fit="fill" />
                    <!-- <i class="el-icon-zoom-in" @click="seeImg"></i>
                    <i class="el-icon-delete" @click="deleteImg"></i> -->
                    <div class="mask">{{ item.detail }}</div>
                </div>
                <span>点赞：{{ item.likeCount }}</span>
                <span style="padding-left: 10px;">收藏：{{ item.collect }}</span>
                <el-button type="danger" :icon="Delete" circle style="height: 25px;width: 25px;" @click="deleteShow(item.id)"/>
            </div>
        </div>

        <div style="margin: 10px 0;">
            <el-pagination v-model:current-page="pageNum" v-model:page-size='pageSize' :page-sizes="[5, 10, 20]"
                layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="onSizeChange"
                @current-change="onCurrentChange" />
        </div>
    </div>





    <el-drawer v-model="drawer" :direction="rtl">
        <template #header>
            <h2>发布民族文化展品</h2>
        </template>
        <template #default>
            <el-form :model="form" label-width="80px" :rules="rules">
                <el-form-item prop="url">
                    <el-upload action="http://localhost:9999/files/uploadFile" :show-file-list="false" :limit="1"
                        :on-remove="handleRemove" class="avatar-uploader" :on-success="uploadSuccess"
                        :on-error="uploadError" :before-upload="beforeAvatarUpload">
                        <img v-if="form.url" :src="form.url" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                            <spvn style="font-size:10px">点击上传图片</spvn>
                        </el-icon>
                    </el-upload>
                </el-form-item>
                <el-form-item prop="classify" label="民族">
                    <el-select v-model="form.classify" placeholder="请选择民族" size="large">
                        <el-option v-for="item in nationalities" :label="item" :value="item" />
                    </el-select>
                </el-form-item>
                <el-form-item prop="detail" label="描述">
                    <el-input v-model="form.detail" autosize type="textarea" placeholder="请输入描述"
                        :autosize="{ minRows: 1, maxRows: 5 }" />
                </el-form-item>
            </el-form>
        </template>

        <template #footer>
            <div style="flex: auto">
                <el-button @click="cancelClick">取消</el-button>
                <el-button type="primary" @click="confirmClick">发布</el-button>
            </div>
        </template>
    </el-drawer>


</template>


<style scoped>
.avatar-uploader .avatar {
    width: 90px;
    height: 90px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #4a4b4d;
    width: 90px;
    height: 90px;
    text-align: center;
}

.container {
    width: 100%;
    overflow: auto;
}

.item {
    float: left;
    box-sizing: border-box;
    padding: 20px;
}

.mask {
    position: absolute;
    width: 100%;
    height: 100%;
    bottom: 0;
    background: rgba(101, 101, 101, 0.7);
    color: #ffffff;
    opacity: 0;
    font-family: "楷体"
        /* line-height: 300px; */
}

.mask-div {
    position: relative;
}

.mask-div:hover .mask {
    opacity: 1;
    cursor: default;
}
</style>